package com.hz.framework.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OPTableDAOImpl implements OPTableDAO
{
	private DBQ db ;
	
	//对表添加字段
	public boolean addField(String tableName, List field) throws SQLException 
	{
		db = new DBQ() ;
		boolean flag = false ;
		String sql ;
		
		if("".equals(tableName) || field == null)
		{
			return flag ;
		}
		
		int q = -1 ;
		
		if(field.size() > 0 )
		{
			for (int i = 0 ; i < field.size() ; i ++)
			{
				String temp = field.get(i).toString() ;
				if(!("".equals(temp)))
				{
					sql = "alter table "+ tableName + " add " + temp + " varchar(200)" ;
					try
					{
						q = db.update(sql) ;
					} 
					catch (SQLException e)
					{
						e.printStackTrace();
						continue ;
					}
					
					if(q == 0)
					{
						flag = true ;
					}
					else
					{
						flag = false ;
					}
				}
			}
		}
		
		db.close() ;
		
		return flag;
	}
	
	//建表,id int,flag varchar(10)为固定值
	public boolean createTable(String tableName, List field) throws SQLException 
	{
		db = new DBQ() ;
		boolean flag = false ;
		
		if("".equals(tableName) || field == null)
		{
			return flag ;
		}
		
		StringBuffer sql = new StringBuffer("create table ") ;
		String tempSql = "( id int primary key identity , flag varchar(10) default 'n' , " ; 
		sql.append(tableName) ;
		sql.append(tempSql) ;
		
		if(field.size() > 0 )
		{
			for (int i = 0 ; i < field.size() ; i ++)
			{
				String temp = field.get(i).toString() ;
				if(!("".equals(temp)))
				{
					sql.append(temp) ;
					sql.append(" varchar(200) , ") ;
				}
			}
		}
		
		sql.append(" ) ") ;
		
		int q = -1;
		try
		{
			q = db.update(sql.toString());
		} 
		catch (SQLException e)
		{
			e.printStackTrace();
			return flag;
		}
		finally
		{
			db.close() ;
		}
		
		if(q == 0)
		{
			flag = true ;
		}
		
		return flag;
	}
	
	//修改表,删除字段
	public boolean delField(String tableName, List field) throws SQLException
	{
		db = new DBQ() ;
		boolean flag = false ;
		String sql ;
		
		if("".equals(tableName) || field == null)
		{
			return flag ;
		}
		
		int q = -1 ;
		
		if(field.size() > 0 )
		{
			for (int i = 0 ; i < field.size() ; i ++)
			{
				String temp = field.get(i).toString() ;
				if(!("".equals(temp)))
				{
					sql = "alter table "+ tableName + " drop column " + temp ;
					try
					{
						q = db.update(sql) ;
					} 
					catch (SQLException e)
					{
						e.printStackTrace();
						continue ;
					}
					
					if(q == 0)
					{
						flag = true ;
					}
					else
					{
						flag = false ;
					}
				}
			}
		}
		
		db.close() ;
		
		return flag;
	}
	
	//删除表
	public boolean dropTable(String tableName) throws SQLException
	{
		boolean flag = false ; 
		db = new DBQ() ;
		
		String sql = "drop table " + tableName ;
		
		int q = -1 ;
		
		try
		{
			q = db.update(sql) ;
			flag=true;
			return flag ;
		} 
		catch (SQLException e)
		{
			e.printStackTrace() ;
			flag = false ; 
			return flag ;
		}
		finally
		{
			db.close() ;
		}
	}

	public static void main(String[] arg) throws SQLException
	{
		OPTableDAO itd = new OPTableDAOImpl() ;
		String table = "test" ;
		List l = new ArrayList() ;
		l.add("ww") ;
		l.add("aa");
		System.out.println(itd.createTable(table, l));
//		System.out.println(itd.delField(table, l)) ;
		
		//itd.dropTable("test") ;
	}
	
}

